package com.quanta.edaily.Service.impl;

import com.quanta.edaily.Service.ArticleService;
import com.quanta.edaily.bean.contants.ResultCode;
import com.quanta.edaily.config.exception.ServiceException;
import com.quanta.edaily.mapper.ArticleMapper;
import com.quanta.edaily.pojo.Article;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public int insert(Article article) {
        return articleMapper.insert(article);
    }

    @Override
    public int deleteById(Integer id) {
        return articleMapper.deleteById(id);
    }

    @Override
    public int updateById(Article article) {
        int i = 0;
        try {
            i = articleMapper.updateById(article);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.PARAM_ERROR, "参数错误");
        }
        return i;
    }

    @Override
    public Article getById(Integer id) {
        Article article = articleMapper.getById(id);
        if (article == null) return null;
        article.setReadCount(article.getReadCount() == null ? 0 : article.getReadCount() + 1);
        updateById(article);
        return article;
    }

    @Override
    public List<Article> get(Integer pageNum, Integer pageSize, String title) {
        return articleMapper.get(pageNum, pageSize, title);
    }
}
